package controller

import (
	"github.com/gin-gonic/gin"
	"hanyun-admin/src/entity"
	"hanyun-admin/src/service"
	"net/http"
	"strconv"
	"time"
)
//菜单列表
func GetMenuList(c *gin.Context)  {
	var menu entity.Menu
	c.Bind(&menu)
	menuList := service.SelectMenuList(menu)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"data":menuList,
		"msg":"操作成功",
	})
}
//新增
func AddSysMenu(c *gin.Context)  {
	var menu entity.Menu
	c.BindJSON(&menu)
	menu.CreateTime=time.Now()
	err := service.InsertSysMenuData(menu)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功！",
	})
}
//查询菜单详情
func GetSysMenuInfo(c *gin.Context)  {
	menuId,_ := strconv.ParseInt(c.Param("menuId"),10,64)
	menu := service.SelectSysMenuByMenuId(menuId)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"data":menu,
		"msg":"操作成功",
	})
}
//更新数据
func UpdateSysMenuData(c *gin.Context)  {
	var menu entity.Menu
	c.BindJSON(&menu)
	err := service.UpdateSysMenuData(menu)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//删除
func DeleteSysMenu(c *gin.Context)  {
	menuId, _ := strconv.ParseInt(c.Param("menuId"),10,64)
	err := service.DeleteSysMenuByMenuId(menuId)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}